<?php
class Level extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model("Mlevel");
	}
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_level'),base_url().'admincp/level');
		if($this->input->is_ajax_request()){
            $output = $this->Mlevel->listLevel();
            $this->output->set_output(json_encode($output));
        }else{
            $data['template']="level/view";
            $data['title_page']='<i class="icon20 i-vcard"></i> '.trans('title_level');
			$this->load->view("admincp/template",$data);
        }
	}

	public function add(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_level'),base_url().'admincp/level');
		$this->breadcrumb->append_crumb(trans('breadcrumb_level_add'),base_url().'admincp/level/add');
		$data['template']="level/add";
		$data['title_page']='<i class="icon20 i-plus-circle-2"></i> '.trans('title_level_add');
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_insert = array(
				'level_name' => $this->input->post('level_name'),
				'date_add' => date('Y-m-d',time()), 
			);
			$this->Mlevel->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(base_url()."admincp/level");	
		}
		$this->load->view("admincp/template",$data);
	}

	public function edit(){
		$id = $this->uri->segment(5);
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_level'),base_url().'admincp/level');
		$this->breadcrumb->append_crumb(trans('breadcrumb_level_edit'),base_url().'admincp/level/edit/id/'.$id);
		$data['template']="level/edit";
		$data['title_page']='<i class="icon20 i-pen-2"></i> '.trans('title_level_edit');
		if($id != 1){
			$data['info'] = $this->Mlevel->get_data($id);
			if($this->input->server('REQUEST_METHOD') == "POST"){
				$data_update = array(
					'level_name' => $this->input->post('level_name'),
					'date_edit' => date('Y-m-d',time()), 
				);
				$this->Mlevel->update($data_update,$id);
				$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
				redirect(base_url()."admincp/level");		
			}
		}else{
			$this->session->set_flashdata(array("warning"=>trans('message_edit_can_not')));
			redirect(base_url()."admincp/level");
		}
		$this->load->view("admincp/template",$data);
	}

	public function delete(){
		$id = $this->uri->segment(5);
		if($id != 1){
			$this->Mlevel->delete($id);
			$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		}else{
			$this->session->set_flashdata(array("warning"=>trans('message_delete_can_not')));
		}
		redirect(base_url()."admincp/level");		
	}
}